using System;
using System.Collections.Generic;
using System.Text;

namespace GrooveSPFiles
{
    public class GrooveSPFiles : CommandUtils.GrooveUtilBase
    {
        private string m_SpaceURI;
        private string m_ToolURI;
        private string m_DocumentLibraryURL;
        private string m_SynchronizerIdentity;

        [STAThread]
        static void Main(string[] i_Arguments)
        {
            CommandUtils.UtilAppMain.RunApplication(new GrooveSPFiles(i_Arguments));
        }

        public GrooveSPFiles(string[] i_Arguments)
			: base (i_Arguments)
		{
            base.UsageMessage = "GrooveSPFiles [PARAMS...]";
			base.HelpMessage =
                "GrooveSPFiles\r\n\t" + 
				"Sets properties on specified WSS Space\r\n" +
				"Parameters:\r\n\t" +
                "/Tool=               The Web Services URI for the Groove SharePoint Files tool\r\n\t" +
                "/DocumentLibraryURL= The URL of the SharePoint Document Library\r\n\t" +
                "/SynchronizerIdentity= The Web Services URI for the Synchronizer Groove identity\r\n\t" + 
				"[/Account=           The Web Services URI for the Groove account]\r\n\t" +
				"[/Identity=          The Web Services URI for the Groove identity]\r\n\t" +
				"[/RequestKey=        Token used to communicate Groove host]\r\n\t" +
				"[/Host=              The remote Groove host name to issue commands to]\r\n\t" +
				"[/Port=              The remote Groove host port to issue commands to]\r\n\t" +
				"[/Timeout=           Timeout (in ms) for Web Services method execution]\r\n\t" +
				"[/X=                 Configuration file to use for parameter input]\r\n\t" +
				"\r\n";
		}

        public override CheckResult CheckParameters()
        {
            CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
            if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded)
                return CheckResult;

            m_ToolURI = base.GetParameterValue("Tool");
            if (System.String.IsNullOrEmpty(m_ToolURI))
            {
                base.DisplayUsage("You must specify a tool.");
                return CommandUtils.UtilBase.CheckResult.Failed;
            }

            //  The format for a Tool URI is:
            //    "/GWS/Groove/2.0/Tools/grooveTelespace/fuzvzuaax9wjv72dwfa2sxyd4vabu8bae74s9k2/ToolContainer/vk2xck8tanv5i"
            //
            //  The format for a Space URI is:
            //    "/GWS/Groove/2.0/Spaces/grooveTelespace/fuzvzuaax9wjv72dwfa2sxyd4vabu8bae74s9k2"

            string[] FormsToolURIParts = m_ToolURI.Split(new char[] { '/' });
            m_SpaceURI = "/GWS/Groove/2.0/Spaces/grooveTelespace/" + FormsToolURIParts[6];

            m_DocumentLibraryURL = base.GetParameterValue("DocumentLibraryURL");
            if (System.String.IsNullOrEmpty(m_DocumentLibraryURL))
            {
                base.DisplayUsage("You must specify a Document Library URL.");
                return CommandUtils.UtilBase.CheckResult.Failed;
            }

            m_SynchronizerIdentity = base.GetParameterValue("SynchronizerIdentity");
            if (System.String.IsNullOrEmpty(m_SynchronizerIdentity))
            {
                base.DisplayUsage("You must specify a Synchronizer Groove identity.");
                return CommandUtils.UtilBase.CheckResult.Failed;
            }

            return base.CheckUnusedParameters();
        }

        public override CommandUtils.UtilBase.CheckResult Run()
        {
            GrooveWebServicesV12Helpers.Space Space = null;
            GrooveWebServicesV12Helpers.Tool Tool = null;
            GrooveWebServicesV12Helpers.SharepointFiles.SharepointFilesTool SpFilesTool = null;

            try
            {
                Space = base.Identity.OpenSpace(m_SpaceURI);
            }
            catch (System.Exception Exception)
            {
                base.StandardError.WriteLine("Unable to open space: " + Exception.Message);
                throw Exception;
            }

			try
			{
				Tool = Space.OpenTool(m_ToolURI);
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Unable to open tool: " + Exception.Message);
				throw Exception;
			}

			try
			{
                SpFilesTool = (GrooveWebServicesV12Helpers.SharepointFiles.SharepointFilesTool)Tool;
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Tool is not a SharePoint files tool: " + Exception.Message);
				throw Exception;
			}

            try
            {
                SpFilesTool.SetInitialConfiguration(m_DocumentLibraryURL, m_SynchronizerIdentity);
            }
            catch (System.Exception Exception)
            {
                base.StandardError.WriteLine("Can't set initial configuration of SharePoint files tool: " + Exception.Message);
                throw Exception;
            }

            return CommandUtils.UtilBase.CheckResult.Succeeded;
        }
    }
}
